package cn.loan.tools;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: MaoSuyu
 * @User：John
 * @Date: 2019/5/13
 * @Time: 13:09
 * @Description: No Description
 */
public class FastDfsTool {

    /**
     * 防止被实例化
     */
    private FastDfsTool() {
    }

    private static StorageClient storageClient = null;
    private static StorageClient1 storageClient1 = null;
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;

    /**
     * 加载配置
     */
    static {
        try {
            //加载配置文件
            ClientGlobal.init("vip.conf");
            //得到跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            //得到服务端
            trackerServer = trackerClient.getConnection();
            storageServer = null;
            //得到存储服务器对象
            storageClient = new StorageClient(trackerServer, storageServer);
            storageClient1 = new StorageClient1(trackerServer, storageServer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 存储
     *
     * @param localFile
     * @param fileType
     * @param nameValuePairs
     * @return
     */
    synchronized public static final String[] save(String localFile, String fileType, NameValuePair[] nameValuePairs) {
        String[] info = null;
        try {
            info = storageClient.upload_file(localFile, fileType, nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return info;
    }

    /**
     * 存储
     *
     * @param bytes
     * @param fileType
     * @param nameValuePairs
     * @return
     */
    synchronized public static final String[] save(byte[] bytes, String fileType, NameValuePair[] nameValuePairs) {
        String[] info = null;
        try {
            info = storageClient.upload_file(bytes, fileType, nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return info;
    }

    /**
     * 存储
     *
     * @param localFile
     * @param fileType
     * @param nameValuePairs
     * @return
     */
    synchronized public static final String save1(String localFile, String fileType, NameValuePair[] nameValuePairs) {
        String info = null;
        try {
            info = storageClient1.upload_file1(localFile, fileType, nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return info;
    }

    /**
     * 存储
     *
     * @param bytes
     * @param fileType
     * @param nameValuePairs
     * @return
     */
    synchronized public static final String save1(byte[] bytes, String fileType, NameValuePair[] nameValuePairs) {
        String info = null;
        try {
            info = storageClient1.upload_file1(bytes, fileType, nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return info;
    }


    /**
     * 得到文件信息
     *
     * @param groupName      组名
     * @param remoteFileName 远程文件名
     * @return
     */
    public static final FileInfo getFileInfo(String groupName, String remoteFileName) {
        FileInfo fileInfo = null;
        try {
            fileInfo = storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return fileInfo;
    }

    /**
     * 得到文件信息
     *
     * @param remoteFilePath 远程文件路径（组名+远程文件名）
     * @return
     */
    public static final FileInfo getFileInfo(String remoteFilePath) {
        FileInfo fileInfo = null;
        try {
            fileInfo = storageClient1.get_file_info1(remoteFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return fileInfo;
    }

    /**
     * 得到metadata信息
     *
     * @param groupName      组名
     * @param remoteFileName 远程文件名
     * @return
     */
    public static final NameValuePair[] getMetaData(String groupName, String remoteFileName) {
        NameValuePair[] nameValuePairs = null;
        try {
            nameValuePairs = storageClient.get_metadata(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
            close();
        }
        return nameValuePairs;
    }

    /**
     * 得到metadata信息
     *
     * @param remoteFilePath 远程文件路径（组名+远程文件名）
     * @return
     */
    public static final NameValuePair[] getMetaData(String remoteFilePath) {
        NameValuePair[] nameValuePairs = null;
        try {
            nameValuePairs = storageClient1.get_metadata1(remoteFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return nameValuePairs;
    }

    /**
     * 删除文件
     *
     * @param groupName      组名
     * @param remoteFileName 远程文件名
     * @return 0:删除成功,22删除失败
     */
    synchronized public static final int delete(String groupName, String remoteFileName) {
        int status = -1;
        try {
            status = storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return status;
    }

    /**
     * 删除文件
     *
     * @param remoteFilePath 远程文件路径（组名+远程文件名）
     * @return 0:删除成功,2删除失败
     */
    synchronized public static final int delete(String remoteFilePath) {
        int status = -1;
        try {
            status = storageClient1.delete_file1(remoteFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return status;
    }


    /**
     * 文件下载
     * @param groupName 组名
     * @param remoteFileName 远程文件路径
     * @return
     */
    synchronized public static final byte[] download(String groupName, String remoteFileName) {
        byte[] fileBytes = null;
        try {
            fileBytes = storageClient.download_file(groupName, remoteFileName);
        } catch (Exception e){
            e.printStackTrace();
        }
        return fileBytes;
    }


    /**
     * 文件下载
     * @param remoteFilePath 组名/远程文件路径（组名+远程文件名）
     * @return
     */
    synchronized public static final byte[] download(String remoteFilePath) {
        byte[] fileBytes = null;
        try {
            fileBytes = storageClient1.download_file1(remoteFilePath);
        } catch (Exception e){
            e.printStackTrace();
        }
        return fileBytes;
    }

    /**
     * 关闭资源
     */
    public static final void close() {
        try {
            if (null != trackerServer) {
                trackerServer.close();
            }
            if (null != storageServer) {
                storageServer.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
